package com.sicnu.cheer.leaugebar.managemodule.service;


import com.sicnu.cheer.leaugebar.managemodule.model.AcademyModel;
import com.sicnu.cheer.leaugebar.managemodule.model.MajorModel;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.List;


public class XmlParserHandler extends DefaultHandler {

	/**
	 * 存储所有的解析对象
	 */
	private List<AcademyModel> academyList = new ArrayList<AcademyModel>();

	public XmlParserHandler() {

	}

	public List<AcademyModel> getDataList() {
		return academyList;
	}

	@Override
	public void startDocument() throws SAXException {
		// 当读到第一个开始标签的时候，会触发这个方法
	}

	AcademyModel academyModel = new AcademyModel();
	MajorModel majorModel = new MajorModel();

	@Override
	public void startElement(String uri, String localName, String qName,
							 Attributes attributes) throws SAXException {
		// 当遇到开始标记的时候，调用这个方法
		if (qName.equals("academy")) {
			academyModel = new AcademyModel();
			academyModel.setName(attributes.getValue(0));
			academyModel.setCityList(new ArrayList<MajorModel>());
		} else if (qName.equals("major")) {
			majorModel = new MajorModel();
			majorModel.setName(attributes.getValue(0));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// 遇到结束标记的时候，会调用这个方法
		if  (qName.equals("major")) {
			academyModel.getCityList().add(majorModel);
		} else if (qName.equals("academy")) {
			academyList.add(academyModel);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

}
